package com.example.jzg.mvpdemo.base;
/**
 * author: gcc
 * date: 2016/12/5 10:53
 * email:
 */

import android.app.Application;
import android.content.Context;
import android.content.res.Resources;

import java.util.concurrent.TimeUnit;

import cat.ereza.customactivityoncrash.CustomActivityOnCrash;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * author: guochen
 * date: 2016/12/5 10:53
 * email: 
 */
public class AppContext extends Application{
    private static AppContext baseApplication;
    public static HttpService httpService;
    public static final String API = "http://jiancejg.sandbox.guchewang.com";
    @Override
    public void onCreate() {
        super.onCreate();
        baseApplication = this;
        initHttp();
        CustomActivityOnCrash.install(this);
    }

    private void initHttp() {
            OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
            OkHttpClient client = httpBuilder
                    .readTimeout(30, TimeUnit.SECONDS)
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS) //设置超时
                    .build();

            Retrofit restAdapter = new Retrofit.Builder().baseUrl(API) //设置请求路径
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(client).build(); //设置请求客户端为OkHttpClient

          httpService = restAdapter.create(HttpService.class);
    }

    public static Context getAppContext() {
        return baseApplication;
    }
    public static Resources getAppResources() {
        return baseApplication.getResources();
    }
    @Override
    public void onTerminate() {
        super.onTerminate();
    }
}
